package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Authoer Pan Rundong
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;
    /**
     * 添加套餐，同时保证套餐与菜品的关联
     * @param setmealDto
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        //1. 保存套餐基本信息，操作setmeal，执行insert操作
        this.save(setmealDto);

        //2. 保存套餐与菜品关联信息，操作setmeal_dish，执行insert操作
        List<SetmealDish> dishList = getSetmealDishes(setmealDto);

        setmealDishService.saveBatch(dishList);
    }

    /**
     * 删除套餐，同时删除套餐与菜品的关联
     * @param ids
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteWithDish(List<Long> ids) {
        //设置条件，根据id查询setmeal表，若有在售套餐则抛出异常
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId, ids);
        queryWrapper.eq(Setmeal::getStatus, 1);

        //查询setmeal表
        int count = this.count(queryWrapper);
        if(count > 0){
            //如果不能删除，则抛出一个业务异常
            throw new CustomException("套餐售卖中，不能删除");
        }

        //若可以删除，则先删除setmeal表
        this.removeByIds(ids);

        //然后根据setmealId，删除setmeal——dish表中相关信息
        LambdaQueryWrapper<SetmealDish> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.in(SetmealDish::getSetmealId,ids);

        setmealDishService.remove(queryWrapper1);
    }

    /**
     * 根据id查询套餐信息
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public SetmealDto getByIdWithDish(Long id) {
        //查询套餐基本信息
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();

        //拷贝
        BeanUtils.copyProperties(setmeal,setmealDto);

        //根据setmeal查询对应菜品
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, id);

        //查询
        List<SetmealDish> list = setmealDishService.list(queryWrapper);

        setmealDto.setSetmealDishes(list);

        return setmealDto;
    }

    /**
     * 修改套餐，同时修改套餐与菜品的关联
     * @param setmealDto
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateWithDish(SetmealDto setmealDto) {
        //修改套餐信息
        this.updateById(setmealDto);

        //根据套餐id删除对应套餐与菜品关联
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());

        setmealDishService.remove(queryWrapper);

        //建立套餐与菜品关联
        List<SetmealDish> setmealDishes = getSetmealDishes(setmealDto);
        //保存菜品与套餐的关联
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 套餐起售停售
     * @param status
     * @param ids
     */
    @Override
    public void changeStatus(Integer status, List<Long> ids) {
        //根据ids查询对应setmeal
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);

        List<Setmeal> setmealList = this.list(queryWrapper);

        //对list里setmeal对象重新设置status
        setmealList = setmealList.stream().map((item)->{
            item.setStatus(status);
            return item;
        }).collect(Collectors.toList());

        //重新修改status
        this.updateBatchById(setmealList);
    }

    /**
     * 根据sql分页查询
     * @Author: Pan Rundong
     * @Date: 2023-02-02 14:15
     * @Greet2u:  （O｀ 3′O）~~♡
     * @param page
     * @param pageSize
     * @param name
     * @return com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.itheima.reggie.dto.DishDto>
     */
    @Override
    public Page<SetmealDto> pageBySql(int page, int pageSize, String name) {
        //构建分页构造器
        Page<SetmealDto> dtoPage = new Page<>(page,pageSize);

        //分页查询
        this.getBaseMapper().pageBySql(dtoPage, name);

        return dtoPage;
    }

    /**
     * 存入套餐id
     * @param setmealDto
     * @return List
     */
    private static List<SetmealDish> getSetmealDishes(SetmealDto setmealDto) {
        List<SetmealDish> dishList = setmealDto.getSetmealDishes();
        dishList = dishList.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        return dishList;
    }
}
